package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataTree;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.ExamineItemType;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.service.ExamineItemTypeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: ExamineItemTypeController
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2019/12/11 14:54
 * @Version: 1.0
 */
@Controller
public class ExamineItemTypeController {
    @Resource
    private ExamineItemTypeService examineItemTypeService;

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取“项目类型树”的数据
     * @Date: 2020/1/2 11:44
     * @Param: []
     * @Return: java.util.List<java.lang.Object>
     **/
    @ResponseBody
    @RequestMapping("/getTypeTreeData")
    public List<Object> getTypeTreeData(){
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        long unitId=examiner.getUnitId();
        List<Object> dataTreeList=examineItemTypeService.getTypeTreeData(unitId);
        System.out.println("dataTreeList:"+dataTreeList);
        return dataTreeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 更新‘项目类型树’节点的名称
     * @Date: 2020/1/2 11:44
     * @Param: [dataTree]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/updateNodeName")
    public ResponseResult updateNodeName(@RequestBody DataTree dataTree){
        System.out.println("新的树形节点信息（controller） dataTree:"+dataTree);
        ResponseResult responseResult=examineItemTypeService.updateTreeNodeName(dataTree);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 执行添加‘体检项目类型’操作
     * @Date: 2020/1/2 20:41
     * @Param: [dataTree, typeName]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/addTypeNode")
    public ResponseResult addTypeNode(Integer id, String typeName){
        System.out.println("id:"+id+"\t typeName:"+typeName);
        ResponseResult responseResult=new ResponseResult();
        responseResult=examineItemTypeService.addTypeTreeNode(id, typeName);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据项目类型ID调用删除逻辑执行删除操作（删除该类型及其所有子类型）
     * @Date: 2020/1/4 17:43
     * @Param: [itemTypeId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/deleteItemTypes")
    public ResponseResult deleteItemTypes(Integer itemTypeId){
        ResponseResult responseResult=new ResponseResult();
        responseResult=examineItemTypeService.deleteTypeTreeNodes(itemTypeId);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 添加项目类型树的第一个根节点
     * @Date: 2020/1/8 18:07
     * @Param: [itemTypeName]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/addItemTypeTreeRoot")
    public ResponseResult addItemTypeTreeRoot(String itemTypeName){
        ResponseResult responseResult=new ResponseResult();
        Session session= SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        long unitId=examiner.getUnitId();
        responseResult=examineItemTypeService.addItemTypeTreeRoot(itemTypeName, unitId);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取‘体检项目类型’中科室一级（level==1）的所有记录
     * @Date: 2020/1/8 19:47
     * @Param: []
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItemType>
     **/
    @ResponseBody
    @RequestMapping("/getTypeLevel1List")
    public List<ExamineItemType> getTypeLevel1List(){
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        long unitId=examiner.getUnitId();
        return examineItemTypeService.getItemTypeLevel1List(unitId);
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据一级类型（level==1）中选中的类型ID获取‘体检项目类型’二级（level==2）的所有记录
     * @Date: 2020/1/10 22:38
     * @Param: [itemTypeId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItemType>
     **/
    @ResponseBody
    @RequestMapping("/getTypeLevel2List")
    public List<ExamineItemType> getTypeLevel2List(int itemTypeId){
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        long unitId=examiner.getUnitId();
        List<ExamineItemType> examineItemTypeList=examineItemTypeService.getItemTypeLevel2List(itemTypeId, unitId);
        return examineItemTypeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据二级类型（level==2）中选中的类型ID获取本单位（unitId确定）‘体检项目类型’三级（level==3）的所有记录
     * @Date: 2020/1/10 22:39
     * @Param: [itemTypeId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItemType>
     **/
    @ResponseBody
    @RequestMapping("/getTypeLevel3List")
    public List<ExamineItemType> getTypeLevel3List(int itemTypeId){
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        long unitId=examiner.getUnitId();
        List<ExamineItemType> examineItemTypeList=examineItemTypeService.getItemTypeLevel3List(itemTypeId, unitId);
        return examineItemTypeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据二级类型（level==3）中选中的类型ID获取本单位（unitId确定）‘体检项目类型’四级（level==4）的所有记录
     * @Date: 2020/1/11 14:26
     * @Param: [itemTypeId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItemType>
     **/
    @ResponseBody
    @RequestMapping("/getTypeLevel4List")
    public List<ExamineItemType> getTypeLevel4List(int itemTypeId){
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        long unitId=examiner.getUnitId();
        List<ExamineItemType> examineItemTypeList=examineItemTypeService.getItemTypeLevel4List(itemTypeId, unitId);
        return examineItemTypeList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据examineItemType获取相应的examineItemType
     * @Date: 2020/3/20 22:58
     * @Param: [itemTypeId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/getItemTypeById")
    public ResponseResult getItemTypeById(Integer itemTypeId){
        ResponseResult responseResult=examineItemTypeService.getItemTypeById(itemTypeId);
        return responseResult;
    }

}
